﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JGSY.LHBS
{
    public class CommonHelper
    {
        //存储路径
        public static string storePath = "";
        //配置文件的路径
        public static string setFilePath = "";
        //名目文件路径
        public static string itemPath = "";
        //收入信息文件路径 
        public static string incomePath = "";
        //支出信息文件路径
        public static string expendPath = "";
        //日统计文件路径
        public static string dayPath = "";
        //周统计文件路径
        public static string weekPath = "";
        //月统计文件路径
        public static string monthPath = "";



        /// <summary>
        /// 获取指定类型的名目列表
        /// </summary>
        /// <param name="itemType">1 收入  2 支出</param>
        /// <returns></returns>
        public static List<string> GetItems(int itemType)
        {
            List<string> items = new List<string>();
            string itemPath = CommonHelper.itemPath;
            if (File.Exists(itemPath))
            {
                List<string> lines = TextHelper.ReadTextAllLines(itemPath);
                //大于1行才有数据
                if (lines.Count > 1)
                {
                    string typeName = itemType == 1 ? "收入" : "支出";
                    for (int i = 1; i < lines.Count; i++)
                    {
                        //编号;类型;名称
                        string[] arr = lines[i].Split(';');
                        if (arr[1] == typeName)
                        {
                            items.Add(arr[2]);
                        }
                    }
                }
            }
            return items;
        }

        /// <summary>
        /// 获取所有的收入记录信息
        /// </summary>
        /// <returns></returns>
        public static List<IncomeInfo> GetIncomeList()
        {
            List<IncomeInfo> list = new List<IncomeInfo>();
            string incomePath = CommonHelper.incomePath;
            if (File.Exists(incomePath))
            {
                List<string> lines = TextHelper.ReadTextAllLines(incomePath);
                if (lines.Count > 1)
                {
                    for (int i = 1; i < lines.Count; i++)
                    {
                        //编号;名目;描述;金额;记录时间
                        string[] arr = lines[i].Split(';');
                        IncomeInfo info = new IncomeInfo()
                        {
                            IncomeId = int.Parse(arr[0]),
                            ItemName = arr[1],
                            IncomeDesp = arr[2],
                            Money = decimal.Parse(arr[3]),
                            IncomeTime = arr[4]
                        };
                        list.Add(info);
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 获取所有的支出记录信息
        /// </summary>
        /// <returns></returns>
        public static List<ExpendInfo> GetExpendList()
        {
            List<ExpendInfo> list = new List<ExpendInfo>();
            string expendPath = CommonHelper.expendPath;
            if (File.Exists(expendPath))
            {
                List<string> lines = TextHelper.ReadTextAllLines(expendPath);
                if (lines.Count > 1)
                {
                    for (int i = 1; i < lines.Count; i++)
                    {
                        //编号;名目;描述;金额;记录时间
                        string[] arr = lines[i].Split(';');
                        ExpendInfo info = new ExpendInfo()
                        {
                            ExpendId = int.Parse(arr[0]),
                            ItemName = arr[1],
                            ExpendDesp = arr[2],
                            Money = decimal.Parse(arr[3]),
                            ExpendTime = arr[4]
                        };
                        list.Add(info);
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 获取所有的收支记录
        /// </summary>
        /// <returns></returns>
        public static List<RecordInfo> GetAllRecordList()
        {
            List<RecordInfo> records = new List<RecordInfo>();
            List<IncomeInfo> incomeList = GetIncomeList();
            List<ExpendInfo> expendList = GetExpendList();
            int id = 0;
            if (incomeList.Count > 0)
            {
                incomeList.ForEach(r =>
                {
                    id += 1;
                    records.Add(new RecordInfo()
                    {
                        Id = id,
                        ItemType = "收入",
                        ItemName = r.ItemName,
                        Description = r.IncomeDesp,
                        Money = r.Money,
                        RecordTime = DateTime.Parse(r.IncomeTime)
                    });
                });
            }
            if (expendList.Count > 0)
            {
                expendList.ForEach(r =>
                {
                    id += 1;
                    records.Add(new RecordInfo()
                    {
                        Id = id,
                        ItemType = "支出",
                        ItemName = r.ItemName,
                        Description = r.ExpendDesp,
                        Money = r.Money,
                        RecordTime = DateTime.Parse(r.ExpendTime)
                    });
                });
            }
            return records;
        }


    }
}
